package com.shitou.springai1.user.config;

import com.hankcs.hanlp.HanLP;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Configuration
@Slf4j
public class NLPConfig {

    /**
     * 配置HanLP
     */
    @PostConstruct
    public void initHanLP() {
        try {
            // 获取resources目录的实际路径
            ClassPathResource resource = new ClassPathResource("dictionary/HanLP.txt");
            String dictionaryPath = resource.getURL().getPath();

            // 设置HanLP的配置
            String hanlpProperties = "root=./hanlp\n" +
                    "## 配置自定义词典路径\n" +
                    "custom.dictionary.path=" + dictionaryPath;

            // 写入临时配置文件
            Files.write(Paths.get("./hanlp.properties"), hanlpProperties.getBytes());
            HanLP.Config.enableDebug(false); // 关闭调试信息
            log.info("HanLP配置成功，词典路径：{}", dictionaryPath);
        } catch (IOException e) {
            log.warn("HanLP配置写入失败，使用默认配置", e);
        }
    }
}
